<template>
  <el-dialog
    :title="!dataForm.dictCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="字典排序" prop="dictSort">
      <el-input v-model="dataForm.dictSort" placeholder="字典排序"></el-input>
    </el-form-item>
    <el-form-item label="字典标签" prop="dictLabel">
      <el-input v-model="dataForm.dictLabel" placeholder="字典标签"></el-input>
    </el-form-item>
    <el-form-item label="字典键值" prop="dictValue">
      <el-input v-model="dataForm.dictValue" placeholder="字典键值"></el-input>
    </el-form-item>
    <el-form-item label="字典类型" prop="dictType">
      <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
    </el-form-item>
    <el-form-item label="样式属性（其他样式扩展）" prop="cssClass">
      <el-input v-model="dataForm.cssClass" placeholder="样式属性（其他样式扩展）"></el-input>
    </el-form-item>
    <el-form-item label="表格回显样式" prop="listClass">
      <el-input v-model="dataForm.listClass" placeholder="表格回显样式"></el-input>
    </el-form-item>
    <el-form-item label="是否默认（Y是 N否）" prop="isDefault">
      <el-input v-model="dataForm.isDefault" placeholder="是否默认（Y是 N否）"></el-input>
    </el-form-item>
    <el-form-item label="状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（0正常 1停用）"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          dictCode: 0,
          dictSort: '',
          dictLabel: '',
          dictValue: '',
          dictType: '',
          cssClass: '',
          listClass: '',
          isDefault: '',
          status: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          dictSort: [
            { required: true, message: '字典排序不能为空', trigger: 'blur' }
          ],
          dictLabel: [
            { required: true, message: '字典标签不能为空', trigger: 'blur' }
          ],
          dictValue: [
            { required: true, message: '字典键值不能为空', trigger: 'blur' }
          ],
          dictType: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
          cssClass: [
            { required: true, message: '样式属性（其他样式扩展）不能为空', trigger: 'blur' }
          ],
          listClass: [
            { required: true, message: '表格回显样式不能为空', trigger: 'blur' }
          ],
          isDefault: [
            { required: true, message: '是否默认（Y是 N否）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（0正常 1停用）不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.dictCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dictCode) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysdictdata/info/${this.dataForm.dictCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dictSort = data.sysDictData.dictSort
                this.dataForm.dictLabel = data.sysDictData.dictLabel
                this.dataForm.dictValue = data.sysDictData.dictValue
                this.dataForm.dictType = data.sysDictData.dictType
                this.dataForm.cssClass = data.sysDictData.cssClass
                this.dataForm.listClass = data.sysDictData.listClass
                this.dataForm.isDefault = data.sysDictData.isDefault
                this.dataForm.status = data.sysDictData.status
                this.dataForm.createBy = data.sysDictData.createBy
                this.dataForm.createTime = data.sysDictData.createTime
                this.dataForm.updateBy = data.sysDictData.updateBy
                this.dataForm.updateTime = data.sysDictData.updateTime
                this.dataForm.remark = data.sysDictData.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysdictdata/${!this.dataForm.dictCode ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'dictCode': this.dataForm.dictCode || undefined,
                'dictSort': this.dataForm.dictSort,
                'dictLabel': this.dataForm.dictLabel,
                'dictValue': this.dataForm.dictValue,
                'dictType': this.dataForm.dictType,
                'cssClass': this.dataForm.cssClass,
                'listClass': this.dataForm.listClass,
                'isDefault': this.dataForm.isDefault,
                'status': this.dataForm.status,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
